﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ITXHWEB.Controllers
{
    public partial class WBBSTController : ITXController
    {



        [ChildActionOnly]
        public ActionResult SSTKRGBN(string returnUrl)
        {

            ModelState.Clear();

            MABF.CWBB.SLO = "01";
            MABF.CWBB.SLB = "CW";
            MABF.CWBB.LO = "01";
            MABF.CWBB.LB = "CW";
            MABF.CWBB.LP = "01    ";
            MABF.CWBB.WHP = MABF.CWBB.LO + MABF.CWBB.LB + MABF.CWBB.LP;
            MABF.TERMNO = TERMNO;
            MABF.STAFF = CUSER;
            return PartialView(MABF.CWBB);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SSTKRGBN(WBBSTModel model, string returnUrl)
        {
            try
            {
                SETGEBNR(model);//指定布卷的托盘编号  
                StartNewGEBNR();
                ModelState.Clear();
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError("",ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        public void SETGEBNR(WBBSTModel model)
        {
            if (model == null)
                return;
            if (model.WBBSTKNR == null)
                return;
           
            MABF.CWBB.WBBSTKNR = Helper.Left(model.WBBSTKNR.PadRight(12), 12);
            dwData = VHP.VWBB.TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBSTKNR"] = MABF.CWBB.WBBSTKNR.PadRight(12);
            if (MABF.CWBB.ART.Trim().Length>1)
            {
                dwData["WBBART"] = " AND WBBART='" + MABF.CWBB.ART + "' AND WBBVAR='" + MABF.CWBB.VAR + "' AND WBBFARBE='" + MABF.CWBB.FARBE + "'";

            }
            else
            {
                dwData["WBBART"] =""; 
            }
            SResult = VHP.VWBB.QWB13(dwData);
            DataRow dwWB = null;
            foreach (DataRow row in SResult.Tables[0].Copy().Rows)
            {
                dwWB = row;
                break;
            }
            if(dwWB==null)
            {
                INFO = "布卷不存在或者品种信息不一致";
                return;
            }
            Helper.GetSTime(ref date, ref time, ref stime);
            dwWB["WBBAENDBEN"] = MABF.NEWUSER.PadRight(8);
            dwWB["WBBAENDDAT"] = date.PadRight(8);
            dwWB["WBBGEBNR"] = MABF.CWBB.GEBNR.PadRight(8);
            VHP.VWBB.SETGEBNR(dwWB, MABF.CWBB.LO, MABF.CWBB.LB, MABF.CWBB.LP, RUNID);
            MABF.CWBB.WBBSTKNR = "";
        }
        public void StartNewGEBNR()
        {
            MABF.CWBB.GEBNR = "";
            MABF.CWBB.IsGEBNRReady = false;
            MABF.IsPDCReady = false;
        }
    }
}
